package com.cidp.system.service.impl;

import java.io.Serializable;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.cidp.system.common.Constants;
import com.cidp.system.dao.IOperatorDao;
import com.cidp.system.dao.IOperatorDetailDao;
import com.cidp.system.model.Toperator;
import com.cidp.system.model.ToperatorDetail;
import com.cidp.system.service.IOperatorService;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;
import com.litt.core.page.IPageList;
import com.litt.core.page.PageParam;
import com.litt.core.page.PagePoParam;
import com.litt.core.util.RegexUtils;

/**
 * <b>标题：</b>操作员管理Service实现类.
 * <pre><b>描述</b>
 *    操作员管理
 * </pre>
 * 
 * @author <a href="mailto:littcai@hotmail.com">空心大白菜</a>
 * @since 2008-4-10
 * @version 1.0
 */
public class OperatorServiceImpl implements IOperatorService 
{
	private IOperatorDao operatorDao;
	
	private IOperatorDetailDao operatorDetailDao;
	
	/**
	 * 读取操作员信息
	 * @param opId 操作员ID
	 * @return Toperator对象
	 */
	public Toperator load(Long opId)  throws BusiException
	{
		if(opId!=null && !Utility.isEmpty(opId.toString())){
			Toperator operator = (Toperator)operatorDao.loadObject(Toperator.class, opId);
			return operator;
		}else{
			throw new BusiException("用户ID为空字段！");
		}
		
	}
	
	/**
	 * 逻辑删除操作员.
	 * 
	 * @param opId 操作员ID
	 */
	public void deleteLogic(Long opId)  throws BusiException
	{		
		if(opId!=null && !Utility.isEmpty(opId.toString())){
			Toperator operator = this.load(opId);
			operator.setStatus(new Long(2));
			operatorDao.updateObject(operator);
		}else{
			throw new BusiException("用户ID为空字段！");
		}
	}

	/**
	 * 恢复逻辑删除的操作员.
	 * 
	 * @param opId 操作员ID
	 */
	public void resumeLogic(Long opId)  throws BusiException
	{	
		if(opId!=null && !Utility.isEmpty(opId.toString())){
			Toperator operator = this.load(opId);
			operator.setStatus(new Long(0));
			operatorDao.updateObject(operator);
		}else{
			throw new BusiException("用户ID为空字段！");
		}
	}
	
	/**
	 * 操作员在线.
	 * 
	 * @param opId 操作员ID
	 */
	public void online(Long opId)  throws BusiException
	{	
		if(opId!=null && !Utility.isEmpty(opId.toString())){
			Toperator operator = this.load(opId);
			operator.setIsOnline(new Long(1));
			operatorDao.updateObject(operator);
		}else{
			throw new BusiException("用户ID为空字段！");
		}
	}
	
	/**
	 * 操作员不在线.
	 * 
	 * @param opId 操作员ID
	 */
	public void unline(Long opId)  throws BusiException
	{	
		if(opId!=null && !Utility.isEmpty(opId.toString())){
			Toperator operator = this.load(opId);
			operator.setIsOnline(new Long(0));
			operatorDao.updateObject(operator);
		}else{
			throw new BusiException("用户ID为空字段！");
		}
	}
	
	/**
	 * 根据登陆ID查找
	 * @param loginId 登陆ID
	 * @return Toperator对象
	 */
	public Toperator loadByLoginId(String loginId)  throws BusiException{
		if(loginId!=null && !Utility.isEmpty(loginId)){
			return operatorDao.loadByLoginId(loginId);
		}else{
			throw new BusiException("用户登录ID为空字段！");
		}
	}

	/**
	 * 注销操作员
	 * 
	 * @param opId
	 */
	public void logoutLogic(Long opId)  throws BusiException{
		if(opId!=null && !Utility.isEmpty(opId.toString())){
			operatorDao.logoutLogic(opId);
		}else{
			throw new BusiException("用户ID为空字段！");
		}
	}
	
	/**
	 * 不可改变操作员
	 * 
	 * @param opId
	 */
	public void nochangeLogic(Long opId)  throws BusiException{
		if(opId!=null && !Utility.isEmpty(opId.toString())){
			operatorDao.nochangeLogic(opId);
		}else{
			throw new BusiException("用户ID为空字段！");
		}
	}
	
	/**
	 * 分页查询用户
	 * @param pageParam 分页参数对象
	 * 
	 * @return IPageList对象
	 */
	public IPageList listByPage(PagePoParam pagePoParam)  throws BusiException{
		return operatorDao.listByPage(pagePoParam);
	}
	
	/**
     * 根据对象保存.
     * @param Toperator 对象
     * @return Long 主键值
     */
	public Long save(Toperator obj)  throws BusiException
	{
		if(this.loadByLoginId(obj.getLoginId())!=null){
			throw new BusiException("已经存在这个用户！");
		}
		validate(obj);
		return operatorDao.saveObject(obj);
	}
	
	/**
     * 根据对象更新
     * @param Toperator 对象
     */
	public void update(Toperator obj)  throws BusiException{
		
		
		validate(obj);
		operatorDao.updateObject(obj);
	}
	
	/**
     * 根据对象删除
     * @param Toperator 对象
     */
	public void delete(Toperator obj)  throws BusiException{
		operatorDao.deleteObject(obj);
	}
	/**
	 * @param operatorDao the operatorDao to set
	 */
	
	public void validate(Toperator obj) throws BusiException{
		if(obj.getOpName()==null || Utility.isEmpty(obj.getOpName())){
			throw new BusiException("操作员名称为空字段！");
		}
		if(obj.getPassword()==null || Utility.isEmpty(obj.getPassword())){
			throw new BusiException("密码为空字段！");
		}
		if(obj.getMobile()!=null && !Utility.isEmpty(obj.getMobile())){
			boolean isMobile = Pattern.matches(RegexUtils.phone_regexp,obj.getMobile());
			if(isMobile==false){
				throw new BusiException("手机号码不符合规范！");
			}
		}
		if(obj.getEmail()!=null && !Utility.isEmpty(obj.getEmail())){
			boolean isEmail = Pattern.matches(RegexUtils.email_regexp,obj.getEmail());
			if(isEmail==false){
				throw new BusiException("Email地址不符合规范！");
			}
		}
		
	}
	
	public List loadRight(String opName) throws BusiException{
		return operatorDao.loadRight(opName);
	}
	/**
	 * 删除记录
	 */
	public void deleteById(Long id)throws BusiException{
		Toperator operator=this.load(id);
		String loginId=operator.getLoginId();
		operatorDao.deleteObject(operator);
		operatorDetailDao.deleteById(loginId);
	}
	public void doLogOut(String loginId,HttpServletRequest request) throws BusiException{
		Toperator operator=operatorDao.loadByLoginId(loginId);
		operator.setIsOnline(new Long(0));
		operatorDao.updateObject(operator);
		HttpSession session=request.getSession();
		session.removeAttribute("loginOper");
	}
	public boolean doIsLongId(String loginId,String loginPassword) throws BusiException{
		return operatorDao.doIsLongId(loginId,loginPassword);
	}
	public boolean doIsLongById(String loginId) throws BusiException{
		return operatorDao.doIsLongById(loginId);
	}
	public void setOperatorDao(IOperatorDao operatorDao){
		this.operatorDao = operatorDao;
	}

	public void setOperatorDetailDao(IOperatorDetailDao operatorDetailDao) {
		this.operatorDetailDao = operatorDetailDao;
	}
}
